﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Script.Serialization;
using FileServerProject;
using Telerik.OpenAccess;

namespace GetFilesAndFoldersData
{
    public class FilesAndFoldersService : IFilesAndFoldersService
    {
        public FilesAndFolders[] GetFilesAndFoldersData(Page page)
        {
            int? folderID=page.FolderID;
            int currentPage=page.CurrentPage;
            int itemsPerPage = page.itemsPerPage;

            FilesAndFolders[] result = new FilesAndFolders[10];// UserOperations.GetFilesAndFoldersData(folderID, currentPage, itemsPerPage);
            return result;
        }

        public bool RegisterUser(CompositeUser user) 
        {
            string userName = user.Username;
            string password = user.Password;
            bool userExists=!CheckUserData(user);
            if (userExists)
            {
                User newUser = new User();
                newUser.UserName = userName;
                newUser.Password = password;
                UserOperations.AddUpdateUser(newUser);
                return true;
            }
            return false;
        }

        public bool CheckUserData(CompositeUser user)
        {
            string userName = user.Username;
            string password = user.Password;

            User searchedUser = new User();
            searchedUser.UserName = userName;
            searchedUser.Password = password;
            if (UserOperations.SelectUser(searchedUser)!=null)
            {
                return true;
            }
            return false;
        }

        public void CreateFolder(CompositeFileOrFolder folder)
        {
            string folderName = folder.Name;
            User owner = folder.Owner;
            FilesAndFolders parent = folder.Parrent;

            FilesAndFolders newFolder = new FilesAndFolders();
            newFolder.Name = folderName;
            newFolder.OwnerID = owner.UserID;
            newFolder.DateCreated = DateTime.Now;
            if (parent!=null)
            {
                newFolder.ParentID = parent.ID;
            }
            else
            {
                newFolder.ParentID = null;
            }
            UserOperations.AddUpdateFolder(newFolder);
        }

        public FolderFolders CreateFolder(FolderFolders folderFolder)
        {
            FolderFolders createdFolder = new FolderFolders();
            FilesAndFolders parent = UserOperations.GetFolderByID(folderFolder.FolderID);

            FilesAndFolders newFolder = new FilesAndFolders();
            newFolder.Name = folderFolder.FolderName;
            newFolder.OwnerID = parent.OwnerID;
            newFolder.DateCreated = DateTime.Now;
            if (parent != null)
            {
                newFolder.ParentID = parent.ID;
            }
            else
            {
                newFolder.ParentID = null;
            }
            UserOperations.AddUpdateFolder(newFolder);
            createdFolder.FolderID = newFolder.ID;
            createdFolder.FolderName = newFolder.Name;

            return createdFolder;
        }

        public bool RenameFolder(CompositeFileOrFolder folder)
        {
            FilesAndFolders newFolder = folder.FileOrFolder;
            string newName = folder.Name;
            FilesAndFolders updateFolder = UserOperations.SelectFolder(newFolder);
            if (updateFolder!=null)
            {
                updateFolder.Name = newName;
                UserOperations.AddUpdateFolder(newFolder);
                return true;
            }
            return false;
        }

        public void DeleteFolder(FilesAndFolders folder)
        {
            UserOperations.DeleteFileOrFolder(folder.ID);
        }

        public bool ChangeParentFolder(CompositeFileOrFolder folder)
        {
            FilesAndFolders fileOrFolder = folder.FileOrFolder;
            FilesAndFolders parent = folder.Parrent;
            FilesAndFolders changeFolder = UserOperations.SelectFolder(fileOrFolder);
            if (changeFolder != null)
            {
                changeFolder.ParentID = parent.ID;
                return true;
            }
            return false;
        }

        public void CreateFile(CompositeFileOrFolder file)
        {
            string fileName = file.Name;
            User owner = file.Owner;
            FilesAndFolders parent = file.Parrent;
            FilesAndFolders newFile = new FilesAndFolders();
            newFile.Name = fileName;
            newFile.OwnerID = owner.UserID;
            newFile.DateCreated = DateTime.Now;
            newFile.ParentID = parent.ID;
            newFile.DownloadCount = 0;
            newFile.LastDownloadDate = null;
            UserOperations.AddUpdateFile(newFile);
        }

        public bool RenameFile(CompositeFileOrFolder file)
        {
            FilesAndFolders newFile = file.FileOrFolder;
            string newName = file.Name;
            FilesAndFolders updateFile = UserOperations.SelectFolder(newFile);
            if (updateFile != null)
            {
                updateFile.Name = newName;
                UserOperations.AddUpdateFile(newFile);
                return true;
            }
            return false;
        }

        public void DeleteFile(FilesAndFolders file)
        {
            UserOperations.DeleteFileOrFolder(file.ID);
        }

        public List<string> GetAllUsers()
        {
            List<string> userNames = new List<string>();
            List<User> users = UserOperations.GetAllUsers();
            foreach (var user in users)
            {
                userNames.Add(user.UserName);
            }
            return userNames;
        }

        public ParentFolder GetUserFolder(string userName)
        {
            ParentFolder folder = new ParentFolder();
            FilesAndFolders parentFolder = UserOperations.GetUserFolder(userName);
            folder.FolderName=parentFolder.Name;
            folder.FolderID = parentFolder.ID;
            return folder;
        }

        public List<FolderFolders> GetFolderFolders(string parentFolderID)
        {
            List<FilesAndFolders> folderFolders = UserOperations.GetFolderFolders(int.Parse(parentFolderID));
            List<FolderFolders> compositeFolderFolders = new List<FolderFolders>();
            foreach (var folderFolder in folderFolders)
            {
                FolderFolders compositeFolderFolder = new FolderFolders();
                compositeFolderFolder.FolderID = folderFolder.ID;
                compositeFolderFolder.FolderName = folderFolder.Name;
                compositeFolderFolders.Add(compositeFolderFolder);
            }
            return compositeFolderFolders;
        }

        public List<FolderFolders> GetFolderFiles(string parentFolderID)
        {
            List<FilesAndFolders> folderFolders = UserOperations.GetFolderFiles(int.Parse(parentFolderID));
            List<FolderFolders> compositeFolderFolders = new List<FolderFolders>();
            foreach (var folderFolder in folderFolders)
            {
                FolderFolders compositeFolderFolder = new FolderFolders();
                compositeFolderFolder.FolderID = folderFolder.ID;
                compositeFolderFolder.FolderName = folderFolder.Name;
                compositeFolderFolders.Add(compositeFolderFolder);
            }
            return compositeFolderFolders;
        }

        public ParentFolder GetParentFolder(string folderId)
        {
            int folderID = int.Parse(folderId);
            ParentFolder folder = new ParentFolder();
            FilesAndFolders parentFolder = UserOperations.GetParentFolder(folderID);
            folder.FolderName=parentFolder.Name;
            folder.FolderID = parentFolder.ID;
            return folder;
        }
    }
}
